<?php
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;
use PHPExcel_IOFactory;
use think\facade\Env;
use PHPQRCode\QRcode;

class Box extends Common
{
    public $think;
    public $canvas;

    public function __construct()
    {
        parent::__construct();
        $this->think = $_SESSION['think'];
    }

    /*
     *活动列表
    */
    function boxList()
    {
        $inputData = input('get.');
        $res = $list = array();
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];
        $where = 'id > 0';
        if ($this->think['admin_id'] > 1) {
            $where = 'admin_id = ' . $this->think['admin_id'];
        }
        if ($inputData['keyword']) {
            $where .= " and box like '%" . $inputData['keyword'] . "%'";
            $this->assign('keyword', $inputData['keyword']);
        }
        $count = Db::name('box')->where($where)->count();// 查询满足要求的总记录数
        $res = Db::name('box')->where($where)->order('id desc')->paginate($size, $count,
            ['query' => request()->param()])->each(function ($item, $key) {
            return $item;
        });
        $page = $res->render();
        $this->assign('list', $res);// 赋值数据集
        $this->assign('page', $page);// 赋值分页输出
        $this->assign('total', $count);// 数据总数

        return view('boxList');
    }

    /*
       *编辑信息
      */
    public function box()
    {
        $act = input('route.act') ? input('route.act') : 'add';
        $info = array();
        if (input('route.id')) {
            $id = input('route.id');
            $info = db('box')->where('id=' . $id)->find();
            $date = $info['updated_at'];
        } else {
            $date = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
        }

        $this->assign('act', $act);
        $this->assign('info', $info);
        $this->assign('date', $date);// 赋值数据集
        return view();
    }
    /*
       *活动处理
      */
    public function boxHandle()
    {
        $data = input('post.');
        $act = $data['act'];
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        unset($data['act']);
        $r = db('box')->where('port=' . $data['port'])->update(array('port'=>0));
        if ($act == 'add') {
            $data['admin_id'] = $this->think['admin_id'];
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $r = db('box')->insertGetId($data);
            if ($r) {
                $json['status'] = 1;
                $json['message'] = lang('success');
                /*********************记录日志***********************/
                addLog(session('user_name') . '添加活动成功-' . $data['title'] . ' [ ID:' . $r . ' ]');
                echo json_encode($json);
                exit;
            } else {
                $json['status'] = 0;
                $json['message'] =lang('fail');
                echo json_encode($json);
                exit;
            }
        }

        if ($act == 'edit') {
            $id = $data['id'];
            unset($data['id']);
            try {
                $r = db('box')->where('id=' . $id)->update($data);
            } catch (ValidateException $e) {
                // 这是进行验证异常捕获
                return json($e->getError());
            } catch (\Exception $e) {
                // 这是进行异常捕获
                return json($e->getMessage());
            }
            // dump($article_id);exit;
            if ($r !== false) {
                $json['status'] = 1;
                $json['message'] = lang('success');
                /*********************记录日志***********************/
                addLog(session('user_name') . '修改活动成功-' . $data['title'] . ' [ ID:' . $data['article_id'] . ' ]');
                echo json_encode($json);
                exit;
            } else {
                $json['status'] = 0;
                $json['message'] = lang('fail');
                echo json_encode($json);
                exit;
            }

        }

    }

}


?>
